﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace B121B {
    class Program {
        /// <summary>
        /// The collection of vehicles/sellers
        /// </summary>
        private static VehicleCollection VHC { get; set; }

        static void Main(string[] args) {
            PrepareForUse(50, 5);

            while (true) {
                WriteMenu();
                SelectedMenuItem();
            }
        }

        /// <summary>
        /// Performs search assignment 1
        /// </summary>
        private static void Search1() {
            Console.Write("\nInput brand: ");
            string brand = Console.ReadLine();
            Console.Write("Input model: ");
            string model = Console.ReadLine();
            Console.WriteLine();

            List<Vehicle> vehicles = VHC.FindModel(model, false);
            vehicles = VHC.FindBrand(brand, true);
            PrintVehicles(vehicles);
        }

        /// <summary>
        /// Performs search assignment 2
        /// </summary>
        private static void Search2() {
            Console.Write("\nInput Zip code: ");
            string zip = Console.ReadLine();

            List<Vehicle> vehicles = VHC.ForSaleInZip(zip, false);
            PrintVehicles(vehicles);
        }

        /// <summary>
        /// Performs search assignment 3
        /// </summary>
        private static void Search3() {
            Console.WriteLine("\nInput minimum loadcapacity: ");
            string capacity = Console.ReadLine();

            List<Vehicle> vehicles = VHC.MinLoadCap(int.Parse(capacity), false);
            PrintVehicles(vehicles);
        }

        /// <summary>
        /// Performs search assignment 4
        /// </summary>
        private static void Search4() {
            Console.WriteLine("\nMaxmimum price: ");
            string price = Console.ReadLine();

            List<Vehicle> vehicles = VHC.FindPriceBelowOrEqualTo(int.Parse(price), false);
            vehicles = VHC.FindLicenseBiggerThan(DriversLicense.C, true);
            PrintVehicles(vehicles);
        }

        private static void Search5() {
            Console.WriteLine("\nCustom search. Keywords: ");
            Console.WriteLine("brand, model, maxprice, zip, minload, license");
            Console.WriteLine("Example of search: brand=BMW,model=Z4,ZIP=9000");

            string searchTerm = Console.ReadLine();
            string[] searches = searchTerm.Split(',');

            VHC.FillPrevSearchForAdvancedSearch();
            List<Vehicle> foundVehicles = new List<Vehicle>();
            bool isInputOkay = true;

            foreach (string search in searches) {
                if (search.StartsWith("brand=")) {
                    foundVehicles = VHC.FindBrand(search.Split('=').Last(), true);
                } else if (search.StartsWith("model=")) {
                    foundVehicles = VHC.FindModel(search.Split('=').Last(), true);
                } else if (search.StartsWith("maxprice=")) {
                    foundVehicles = VHC.FindPriceBelowOrEqualTo(int.Parse(search.Split('=').Last()), true);
                } else if (search.StartsWith("zip=")) {
                    foundVehicles=VHC.ForSaleInZip(search.Split('=').Last(), true);
                } else if (search.StartsWith("minload=")) {
                    foundVehicles=VHC.MinLoadCap(int.Parse(search.Split('=').Last()), true);
                } else if (search.StartsWith("license=")) {
                    string license=search.Split('=').Last(); // License to search for

                    List<string> licenses = new List<string>();
                    foreach (DriversLicense l in Enum.GetValues(typeof(DriversLicense))) {
                        licenses.Add(l.ToString()); // Convert licenses to strings
                    }

                    isInputOkay = false; // Assume bad license
                    for (int i = 0; i < licenses.Count; i++) { // Is license a valid license?
                        if (licenses[i] == license) {
                            foundVehicles = VHC.FindLicenseBiggerThan((DriversLicense)i, true); // Found it, search
                            isInputOkay = true; // License was okay
                        }
                    }
                } else {
                    isInputOkay = false;
                }
                if (!isInputOkay) { // input was not okay
                    break;
                }
            }

            if (isInputOkay) {
                    PrintVehicles(foundVehicles);
            } else {
                Console.WriteLine("Bad input");
            }
        }

        /// <summary>
        /// Selects a menu item
        /// </summary>
        private static void SelectedMenuItem() {
            ConsoleKeyInfo keyPressed = Console.ReadKey();

            switch (keyPressed.KeyChar) {
                case '1':
                    Search1();
                    break;
                case '2':
                    Search2();
                    break;
                case '3':
                    Search3();
                    break;
                case '4':
                    Search4();
                    break;
                case '5':
                    Search5();
                    break;
                case 'q':
                case 'Q':
                    Environment.Exit(0);
                    break;
                case 'c':
                case 'C':
                    Console.Clear();
                    break;
                case 'E':
                    Console.WriteLine("Awesome easter egg here");
                    break;
                default:
                    break;
            }

            VHC.ResetSearch(); // Reset the search
        }

        /// <summary>
        /// Prints out a menu
        /// </summary>
        private static void WriteMenu() {
            Console.WriteLine("1 - Find vehicles of brand/model");
            Console.WriteLine("2 - Find vehicles for sale in area");
            Console.WriteLine("3 - Find vehicles having a minimum loadcapacity");
            Console.WriteLine("4 - Find vehicles requiring and below a specified price");
            Console.WriteLine("5 - Combine above searches");

            Console.WriteLine("C - Clear screen");
            Console.WriteLine("Q - Quit");
            Console.WriteLine();
        }

        /// <summary>
        /// Prints the vehicles, if any
        /// </summary>
        /// <param name="vehicles">The vehicles to print</param>
        private static void PrintVehicles(List<Vehicle> vehicles) {
            if (vehicles.Count() == 0) {
                Console.WriteLine("No vehicles fit that description\n");
            } else {
                foreach (Vehicle v in vehicles) {
                    Console.WriteLine(v);
                }
            }
        }


        /// <summary>
        /// Prepares the console
        /// </summary>
        private static void PrepareForUse(int vehicles, int sellers) {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Title = "OOP/B121/B";
            VHC = VHC_Instantiate(sellers, vehicles);
        }


        /// <summary>
        /// Create vehicles and sellers
        /// </summary>
        /// <param name="nSellers">Number of sellers</param>
        /// <param name="nVehicles">Number of vehicles</param>
        /// <returns>The VehicleCollection of vehicles and sellers</returns>
        private static VehicleCollection VHC_Instantiate(int nSellers, int nVehicles) {
            VehicleCollection vhc = new VehicleCollection();

            VehicleSellerCreator creator = new VehicleSellerCreator();

            creator.CreateVehiclesAndSellers(vhc, nSellers, nVehicles); // Create vehicles

            return vhc;
        }
    }
}